<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    design
 * @package     base_default
 * @copyright   Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>
<?php
/**
 * Product list template
 *
 * @see Mage_Catalog_Block_Product_List
 */
?>
<?php
    $_productCollection = $this->getLoadedProductCollection();
    
    $_helper = $this->helper('catalog/output');
    $this->addPriceBlockType('simple', 'catalog/product_price', 'catalog/product/price_product_list.phtml');
    $this->addPriceBlockType('configurable', 'catalog/product_price', 'catalog/product/price_product_list.phtml');
?>
<?php if(!$_productCollection->count()): ?>
<p class="note-msg"><?php echo $this->__('There are no products matching the selection.') ?></p>
<?php else: ?>
<?php 
    $_inStockProduct = array();
    $_outStockProduct = array();

    //resort, sold out product last    
    foreach ($_productCollection as $_product){
        if($_product->isSaleable()){
            $_inStockProduct[] = $_product;
        }else{
            $_outStockProduct[] = $_product;
        }
    }
    $_productCollection = array_merge($_inStockProduct, $_outStockProduct);        
?>
<div class="category-products">
    <?php // Grid Mode ?>
    <?php $_collectionSize = count($_productCollection) ?>
    <?php $_columnCount = 3; ?>
    <?php 
        //get list of bestseller product
        $_bestsellerProductCollection = $this->helper('gtgobbler')->getBestsellerItems();
    ?>  
    <?php $i=0; foreach ($_productCollection as $_product): ?>
        <?php if ($i++%$_columnCount==0): ?>
        <ul class="products-grid">
        <?php endif ?>
            <li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>" id="productContainer_<?php echo $_product->getId()?>">
                <a id="productImgWrap_<?php echo $_product->getId()?>" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image">
                    <img id="productImg_<?php echo $_product->getId()?>" src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(223); ?>" width="223" height="220" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" />
                </a>
                <a class="quickview button" href="javascript:void(0);" id="quickviewBtn-<?php echo $_product->getId()?>">
                    <span>quick view</span>
                </a>                
                <?php 
                    $isSoldOut = '';
                    $isLimitedStock = '';
                    $isBestSeller = '';
                    $isNew = '';
                ?> 
                <?php if(!$_product->isSaleable()) $isSoldOut = ' sold-out' ?>
                    
                <?php 
                    $stockModel = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product);
                    $stockQty = $stockModel->getStockQty();             
                    $storeLimitStockQty = Mage::getStoreConfig('gobblersales/limitedstock/qty');
                    if($_product->isSaleable() && $stockQty > 0 && $stockQty < $storeLimitStockQty)
                        $isLimitedStock = ' limited-stock';
                
                ?>          
                <?php
                    if(in_array($_product->getId(), $_bestsellerProductCollection)){
                        if(Mage::registry('current_category')){
                            if(Mage::registry('current_category')->getId() != 11){
                                $isBestSeller = ' best-seller';
                            }
                        }else{
                            $isBestSeller = ' best-seller';
                        }
                    }
                ?>
                <div class="product-status<?php echo $isSoldOut.$isLimitedStock.$isBestSeller?>"></div>
                <h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></a></h2>                
                <?php echo $this->getPriceHtml($_product, true) ?>
                <div class="actions">
                    <?php if($_product->isSaleable()): ?>
                        <div class="increase">
                            <input type="text" readonly="readonly" id="qty-list-<?php echo $_product->getId()?>" value="1"/>
                            <span class="up pointer" onclick="increaseQty(true, false, false, <?php echo $_product->getId()?>, <?php echo $this->helper('gtgobbler/catalog')->getMaxIncreaseQty($_product)?>)">up</span>
                            <span class="down pointer" onclick="decreaseQty(true, false, false, <?php echo $_product->getId()?>)">down</span>
                        </div>
                        <?php if ($_product->getTypeInstance(true)->hasOptions($_product)):?>
                            <button style="width: 168px;" id='mc-<?php echo $_product->getId()?>' class="button arrow-down mcbutton"><span><span>more choices</span></span></button>
                            <?php $configurableAttributes = $_product->getTypeInstance(true)->getConfigurableAttributesAsArray($_product)?>
                            <div class="select-flavour-container-list" id="flavourc-<?php echo $_product->getId()?>">
                            <?php foreach($configurableAttributes as $attribute):?>                             
                                <table>
                                    <col />
                                    <col width="35"/>
                                    <tbody>
                                        <?php $f=1;$count=count($attribute['values']);
                                        foreach($attribute['values'] as $value):?>
                                            <?php
                                                $childProduct = $_product->getTypeInstance(true)->getProductByAttributes(array($attribute['attribute_id'] => $value['value_index']), $_product);
                                                $childProduct = Mage::getModel('catalog/product')->load($childProduct->getId()); 
                                                if(!$childProduct->isSaleable())
                                                    continue;
                                            ?>
                                            <tr <?php if($f == $count) echo 'class="last"'?> onclick="addToCartAjax(<?php echo $_product->getId()?>, true, false, false, <?php echo $attribute['attribute_id']?>, <?php echo $value['value_index']?>)">                                             
                                                <td class="label">                                                  
                                                    <?php echo $value['label']?>                                                    
                                                </td>
                                                <td class="addto">
                                                    <span>add to</span>
                                                    <span class="big">cart</span>
                                                </td>                                               
                                            </tr>
                                        <?php $f++?>
                                        <?php endforeach?>
                                    </tbody>
                                </table>                                
                            <?php endforeach?>
                            </div>                      
                        <?php else:?>
                            <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="addToCartAjax(<?php echo $_product->getId()?>, true, false, false)"><span><span><?php echo $this->__('add to cart') ?></span></span></button>
                        <?php endif?>
                    <?php else: ?>
                        <p class="availability out-of-stock"><span><?php echo $this->__('Sold Out') ?></span></p>
                    <?php endif; ?>
                    <p class="added-notify" id="an-<?php echo $_product->getId()?>"></p>
                </div>                              
            </li>
        <?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
        </ul>
        <?php endif ?>
        <?php endforeach ?>
        <script type="text/javascript">
            decorateGeneric($$('ul.products-grid'), ['odd','even','first','last']);
            function customAddToCart(url, productId, superAttributeId, superAttributeValue){
                var superParam = '';
                if(typeof(superAttributeId) != 'undefined'){
                    superParam = '?' + 'super_attribute[' + superAttributeId + '] = ' + superAttributeValue;
                }
                var addToLink = url+'qty/' + $('qty-'+productId).value + superParam;   
                setLocation(addToLink);
            }       
        </script>    
</div>
<?php endif; ?>    
